package com.tanhua.dubbo.api;

import com.google.errorprone.annotations.Var;
import com.tanhua.domain.db.User;
import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.domain.vo.UserLocationVo;
import com.tanhua.dubbo.api.mongo.UserLocationApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

@Service
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveLocation(Long userId, Double longitude, Double latitude, String addrStr) {

        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);

        if(userLocation==null){ //需要新增
           userLocation = new UserLocation();
           userLocation.setUserId(userId);
           userLocation.setAddress(addrStr);
           userLocation.setLocation(new GeoJsonPoint(longitude,latitude));
            userLocation.setId(new ObjectId());
            userLocation.setCreated(new Date().getTime());
            userLocation.setUpdated(new Date().getTime());
            userLocation.setLastUpdated(new Date().getTime());
           mongoTemplate.save(userLocation);
        }else{
//            Location Address  Updated  LastUpdated where userId
            Update update = new Update();
            update.set("location",new GeoJsonPoint(longitude,latitude));
            update.set("address",addrStr);
            update.set("updated",new Date().getTime());
            update.set("lastUpdated",new Date().getTime());
            mongoTemplate.updateFirst(query,update, UserLocation.class);
        }
    }

    @Override
    public List<UserLocationVo> search(Long userId, Integer distance) {
//        以当前登录人的位置为圆心 以distance为半径画圆 在这个圆里的用户就是我们要的数据
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        GeoJsonPoint location = userLocation.getLocation(); //圆心

//        distance=5000----->5公里（千米）
        Circle circle = new Circle(location, new Distance(distance / 1000, Metrics.KILOMETERS));
        query = new Query(Criteria.where("location").withinSphere(circle));

        List<UserLocation> userLocationList = mongoTemplate.find(query, UserLocation.class);

        List<UserLocationVo> userLocationVos = UserLocationVo.formatToList(userLocationList);

        return userLocationVos;
    }
}
